<template>
  <div>
    <div id="div1_box">
        <h3 id="h3">品类管理</h3>
        <el-button type="success" icon="el-icon-plus" @click="categorys">添加品类</el-button>
    </div>
    <p id="pp1">当前商品分类ID：</p>
    <!-- 表格 -->
    <el-table :data="reslist" stripe style="width: 100%" border>
      <el-table-column prop="id" label="品类ID" width="200"> </el-table-column>
      <el-table-column prop="name" label="品类名称" width="700">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="open(scope.row.id)">修改品类名称</el-button>
          <el-button type="text" size="small">查看其子品类</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[5, 10, 15, 20]"
      :total="list.length"
      :page-size="size"
      :current-page="page"
      @current-change="change"
      @size-change="change1"
    >
    </el-pagination>

    <!-- 修改id的弹框 -->
  </div>
</template>

<script>
import { BASIS, editID } from "../request/http.js";
export default {
  data() {
    return {
      list: [],
      size: 10,
      page: 1,
    };
  },
  mounted() {
    this.DasisList(); //调用
  },
  //   分页器的计算属性
  computed: {
    reslist() {
      return this.list.slice(
        this.size * (this.page - 1),
        this.size * this.page
      );
    },
  },
  methods: {
    //获取品类的数据
    async DasisList() {
      const { data: res } = await BASIS(`/category/get_category.do`);
      console.log(res);
      this.list = res.data;
    },

    //分页器
    change(val) {
      this.page = val;
    },
    change1(a) {
      this.size = a;
    },

    //修改名称
    open(v) {
        // console.log(v.id);
      this.$prompt("请输入新ID", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          editID(
            `/category/set_category_name.do?categoryId=${v}&categoryName=${value}`
          ).then((res) => {
            console.log(res);
            this.DasisList();
          });
          this.$message({
            type: "success",
            message: " 修改成功",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    //添加品类 按钮 跳转路由
    categorys(){
        this.$router.push('/category')
    }
  },
};
</script>

<style lang="scss">
    #h3{
        font-size: 40px;
        margin: 10px;
    }
    #pp1{
        width: 100%;
        margin: 10px;
    }
    #div1_box{
        display: flex;
        justify-content: space-between;
        height:70px;
        align-items: center;
    }
</style>